# -*- coding: utf-8 -*-
"""
Created on Fri Apr 24 10:01:34 2015

@author: hc
"""
'''
Python天天美味(26) - __getattr__与__setattr__ 




__getattr__和__setattr__可以用来对属性的设置和取值进行处理，比如下面的例子：





复制代码
'''
class Book(object):
    def __setattr__(self, name, value):
        if name == 'value':
            object.__setattr__(self, name, value - 100)
        else:
            object.__setattr__(self, name, value)
    def __getattr__(self, name):
        try:
            return object.__getattribute__(name)
        except:
            return name + ' is not found!'
    def __str__(self):
        return self.name + ' cost : ' + str(self.value)

c = Book()
c.name = 'Python'
c.value = 100
print c.name
print c.value
print c
print c.Type

